package com.cpone.cloudweb.utils.bak;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 有关String类型的操作工具: 如对指定string解析等
 * @author Froc
 *
 */
public class StringUtil {
	/**
	 * 解析diffdata_warning表中的warncontent字段
	 *		解析格式: [   [2015-12-01:杭州DC1TG2->杭州DC1TG2局向电话拦截率范围超出阈值最大范围:82.0,实际局向电话拦截率数量为:207], 
	 * 				      [2015-12-01:横浜C网->横浜C网局向电话拦截率范围超出阈值最大范围:82.0,实际局向电话拦截率数量为:2858],
	 * 					 [2015-12-01:横浜TGSG2->横浜TGSG2局向电话拦截率范围超出阈值最大范围:82.0,实际局向电话拦截率数量为:23487]
	 * 				  ]
	 * @author Froc
	 * @param parseStr 需要解析的字符串
	 * @return 将每个局点封装成list集合
	 */
	public static List<String> getListByParseWarnContent(String parseStr){
		if(parseStr==null || "".equals(parseStr)){
			return null;
		}
		List<String> list = new ArrayList<String>();
		String[] parent = parseStr.split("(\\[)([\\d-:]{11})|(\\[\\[)([\\d-:]{11})");
		for (String str: parent) {
			if(str != null && !"".equals(str)){
				String pareByAfter = str.replaceAll("([\\]]{2})|([\\],]{2})", "");//解析后如下
				/*
				 *   杭州DC1TG2->杭州DC1TG2局向电话拦截率范围超出阈值最大范围:82.0,实际局向电话拦截率数量为:207
				 */
				if(pareByAfter != null && !"".equals(pareByAfter)){
					pareByAfter = pareByAfter.trim();
					list.add(pareByAfter);
				}
			}
		}
		return list;
	}
	/**
	 * 解析warncontent格式
	 * @author Froc
	 * @param parseStr 需要解析的字符串
	 * @param size 取局点信息的条数
	 * @return 将每个局点封装成list集合
	 */
	public static List<String> getListByParseWarnContent(String parseStr, int size){
		if(parseStr==null || "".equals(parseStr)){
			return null;
		}
		if(size == 0 || "".equals(size)){
			return null;
		}
		List<String> list = new ArrayList<String>();
		String[] parent = parseStr.split("(\\[)([\\d-:]{11})|(\\[\\[)([\\d-:]{11})");
		int count = 1;//计数
		for (String str: parent) {
			if(str != null && !"".equals(str)){
				String pareByAfter = str.replaceAll("([\\]]{2})|([\\],]{2})", "");//解析后如下
				/*
				 *   杭州DC1TG2->杭州DC1TG2局向电话拦截率范围超出阈值最大范围:82.0,实际局向电话拦截率数量为:207
				 */
				if( count > size){
					return list;
				}
				if(pareByAfter != null && !"".equals(pareByAfter)){
					count ++;
					//去掉最后一个空白
					pareByAfter = pareByAfter.trim();
					list.add(pareByAfter);
				}
			}
		}
		return list;
	}
	
	/**
	 * 将warncontent的内容封装成需要显示的格式(颜色)
	 * @param parseStr 装有warncontent的内容list集合
	 * @param color 拦截率或阈值范围需要显示的颜色
	 * @return 封装后加入span元素的list集合
	 */
	public static List<String> getListParseWarnContentByPage(List<String> parseStr, String color){
		if(parseStr == null || parseStr.size()==0){
			return null;
		}
		//杭州DC1TG2->杭州DC1TG2局向电话拦截率范围超出阈值最大范围:82.0,实际局向电话拦截率数量为:207 , 
		//横浜C网->横浜C网局向电话拦截率范围超出阈值最大范围:82.0,实际局向电话拦截率数量为:2858 
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < parseStr.size(); i++) {
			String temp = parseStr.get(i);
			StringBuffer buffer = new StringBuffer();
			if(temp.contains(",")){
				String[] parseOne = temp.split(",");
				if(parseOne[0] != null && !"".equals(parseOne[0])){
					//阈值范围
					String[] range = parseOne[0].split(":");
					buffer.append(range[0]);
					String page = "：<span style='color:"+color+"'>"+range[1]+"</span>，";
					buffer.append(page);
				}
				if(parseOne[1] != null && !"".equals(parseOne[1])){
					String[] number = parseOne[1].split(":");
					buffer.append(number[0]);
					String page = "：<span style='color:"+color+"'>"+number[1]+"</span>";
					buffer.append(page);
				}
				list.add(buffer.toString());
			}else {
				String[] parseOne = temp.split(":");
				if(parseOne[0] != null && !"".equals(parseOne[0])){
					buffer.append(parseOne[0]);
					String page = "：<span style='color:"+color+"'>"+parseOne[1]+"</span>";
					buffer.append(page);
				}
				list.add(buffer.toString());
			}
		}
		return list;
	}
	
	//test
/*	public static void main(String[] args) {
		String str = "[[2015-12-01:杭州DC1TG2->杭州DC1TG2局向电话拦截率范围超出阈值最大范围:82.0,实际局向电话拦截率数量为:207],"
				+ "[2015-12-01:0横浜C网->横浜C网局向电话拦截率范围超出阈值最大范围:82.0,实际局向电话拦截率数量为:2858],"
				+ "[2015-12-01:1横浜C网->横浜C网局向电话拦截率范围超出阈值最大范围:82.0,实际局向电话拦截率数量为:2858],"
				+ "[2015-12-01:2横浜C网->横浜C网局向电话拦截率范围超出阈值最大范围:82.0,实际局向电话拦截率数量为:2858],"
				+ "[2015-12-01:3横浜C网->横浜C网局向电话拦截率范围超出阈值最大范围:82.0,实际局向电话拦截率数量为:2858],"
				+ "[2015-12-01:横浜TGSG2->横浜TGSG2局向电话拦截率范围超出阈值最大范围:82.0,实际局向电话拦截率数量为:23487]]";
		
		//String parseBefor = "杭州DC1TG2->杭州DC1TG2局向电话拦截率范围超出阈值最大范围:82.0,实际局向电话拦截率数量为:207";
		List<String> list = StringUtil.getListByParseWarnContent(str, 1);
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}
		String[] parent={"杭州DC1TG2->杭州DC1TG2局向电话拦截率范围超出阈值最大范围:82.0%,实际局向电话拦截率数量为:207", 
						"横浜TGSG2->横浜TGSG2局向电话拦截率范围超出阈值最大范围:82.0,实际局向电话拦截率数量为:23487",
						"上海电信武胜局点电话拦截率范围超出阈值最大范围:1.0E-4,实际局点电话拦截率数量为:70221",
						"横浜TGSG2->横浜TGSG2局向电话拦截率范围超出阈值最大范围:100.0,实际局向电话拦截率数量为:23487"};
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < parent.length; i++) {
			list.add(parent[i]);
		}
		String regRange = "(:[\\d]{1,3}\\.[\\d]{1,2}[%,]{1,2})|(:\\d{1,3}\\.\\dE-\\d)|(:\\d{1,3}\\.[\\d]{1,2})";
		String regNumber = "(:[\\d]{1,})$";
		String range = "";
		String number = "";
		Pattern pRange = Pattern.compile(regRange);
		Pattern pNumber = Pattern.compile(regNumber);
		for (int i = 0; i < list.size(); i++) {
			Matcher matcherRange = pRange.matcher(list.get(i));
			while(matcherRange.find()){
				range = matcherRange.group();
				String rangePag = "<span style='color:red;'>"+range+"</span>";
				list.set(i, matcherRange.replaceAll(rangePag));
			}
			
			Matcher matcherNumber = pNumber.matcher(list.get(i));
			while(matcherNumber.find()){
				number = matcherNumber.group();
				String numberPag = "<span style='color:red;'>"+number+"</span>";
				list.set(i, matcherNumber.replaceAll(numberPag));
			}
		}
		System.out.println(list);
		list = StringUtil.getListParseWarnContentByPage(list);
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}
	}*/
}
